//获取应用实例
const app = getApp()

Page({
  hiddenData: {},
  data: {
    isHidden: {},
    answer_words: [],
    answer_length: 0
  },
  onLoad: function (options) {
    var that = this;

    app.validationLogin({
      callback: function () {

        var loginSession = wx.getStorageSync('loginSession')

        if (loginSession) {
          var session_key = loginSession.session_key

          options.session_key = session_key
console.log(options)
          wx.request({
            url: app.globalData.remoteServerPath + 'study/civilService/createTest.do',
            data: options,
            method: 'POST',
            header: { 'content-type': 'application/x-www-form-urlencoded' },
            success: function (res) {
              console.log(res)
              var data = res.data.result;
              var status = res.data.success;

              for (var i = 0; i < data.length; i++) {
                var str = data[i].id;
                if (i == 0) {

                  that.hiddenData[str] = { key: false, index: i, correctAnswerLength: data[i].correctAnswerLength };
                } else {
                  that.hiddenData[str] = { key: true, index: i, correctAnswerLength: data[i].correctAnswerLength };
                }

              }

              that.setData({
                list: data,
                isHidden: that.hiddenData
              })

            },
            fail: function (res) {
              console.log('submit fail');
            }

          })

        }


      }
    })


  },
  rememberVocabulary: function (idValue, isRight) {

    app.validationLogin({
      callback: function () {
        var loginSession = wx.getStorageSync('loginSession')

        if (loginSession) {
          var session_key = loginSession.session_key

          var options = { session_key: session_key, idValue: idValue, isRight: isRight }

          wx.request({
            url: app.globalData.remoteServerPath + 'study/civilService/rememberVocabulary.do',
            data: options,
            method: 'POST',
            header: { 'content-type': 'application/x-www-form-urlencoded' },
            success: function (res) {

            },
            fail: function (res) {
              console.log('submit fail');
            }

          })
        }
      }
    })




  },
  chooseAnswer: function (event) {
    var that = this;

    var answer = event.currentTarget.dataset.answer;
    var ask = event.currentTarget.dataset.ask;
    var idValue = event.currentTarget.dataset.id;
    var instructions = event.currentTarget.dataset.instructions;

    if (app.containString(answer, ask)) {
      //给出正确提示
      app.showToast("正确");
      //播放正确音频
      app.playBackgroundAudio(app.data.audioPath["audio_good"]);
      //记录到数据库中，已经看过这个单词了
      that.rememberVocabulary(idValue, true)

      //下一题
      that.nextCard(idValue);
      
    } else {
      //给出错误提示
      app.showToast("错误");
      //播放错误音频
      app.playBackgroundAudio(app.data.audioPath["audio_error"]);
      //给出错误提示,将正确的答案定义卡片显示出来
      //app.alert(instructions);

      //记录到数据库中，已经看过这个单词了
      that.rememberVocabulary(idValue, false)
      wx.showModal({
        content: instructions,
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            //下一题
            that.nextCard(idValue);
          }
        }
      });
      

    }

    

  },
  checkAnswer: function (event) {
    var that = this;

    var ask = event.currentTarget.dataset.ask;
    var idValue = event.currentTarget.dataset.id;

    //获取用户答案
    var answer_words = that.data.answer_words.join("");

    if (app.equalString(answer_words, ask)) {
      //播放正确音频
      app.playBackgroundAudio(app.data.audioPath["audio_good"]);
      //给出正确提示
      app.showToast("正确");

      //记录到数据库中，已经看过这个单词了
      that.rememberVocabulary(idValue, true)

    } else {
      //播放错误音频
      app.playBackgroundAudio(app.data.audioPath["audio_error"]);
      //给出错误提示,将正确的答案定义卡片显示出来

      //记录到数据库中，已经看过这个单词了
      that.rememberVocabulary(idValue, false)
    }


    //下一题 
    that.nextCard(idValue);

  },
  chooseWord: function (event) {
    var that = this;
    var word = event.currentTarget.dataset.word;

    //判断答案数组长度 是否超长
    if (that.data.answer_words.length < that.data.answer_length) {
      that.data.answer_words.push(word);
    }

    this.setData({
      answer_words: that.data.answer_words
    })
  },
  clearAnswer: function (event) {
    this.data.answer_words.pop()
    //清空选中的项
    this.setData({
      answer_words: this.data.answer_words
    })
  },
  nextCard: function (idValue) {
    var that = this;

    that.hiddenData[idValue].key = true;

    var index = that.hiddenData[idValue].index;

    //遍历数据寻找下一题index
    for (var item in that.hiddenData) {
      if (app.equalNumber(that.hiddenData[item].index, index + 1)) {
        that.hiddenData[item].key = false;
        that.data.answer_length = that.hiddenData[item].correctAnswerLength;
      }
    }

    this.setData({
      isHidden: that.hiddenData,
      answer_length: that.data.answer_length,
      answer_words: []
    })
  }

})
